package com.jieao.template.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jieao.template.common.util.CurrentlyLoggedInUtil;
import com.jieao.template.common.util.JsonUtils;
import com.jieao.template.entity.pub.Pagination;
import com.jieao.template.entity.roleMgr.AssignmentPermissionDTO;
import com.jieao.template.entity.roleMgr.RoleDO;
import com.jieao.template.mapper.RoleMapper;
import com.jieao.template.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 角色表(Role)表服务实现类
 *
 * @author jzw
 * @since 2020-04-30 09:31:15
 */
@Service
public
class RoleServiceImpl implements RoleService
{

    @Resource
    private RoleMapper roleMapper;

    /**
     * 获取当前登录用户的角色
     *
     * @return java.util.List<java.lang.String>
     * @author Jzw
     * @date 2019/12/27 15:40
     */
    @Override
    public List<String> getAllRolesByUserId()
    {
        return roleMapper.getAllRolesByUserId(CurrentlyLoggedInUtil.getUserId());

    }

    /**
     * 给角色分配权限
     *
     * @param assignmentPermissionDTO
     * @return void
     * @author Jzw
     * @date 2019/12/17 16:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void assignmentPermissionForRole(AssignmentPermissionDTO assignmentPermissionDTO)
    {
        List<String> checkList = assignmentPermissionDTO.getCheckList();
        List<String> halfList = assignmentPermissionDTO.getHalfList();
        String id = assignmentPermissionDTO.getId();
        //将半选和全选封装到一起
        List<Map<String, Object>> dataList = new LinkedList<>();
        checkList.forEach(ele -> {
            Map<String, Object> dataMap = new HashMap<>(3);
            dataMap.put("permissionId", ele);
            dataMap.put("roleId", id);
            dataMap.put("checked", true);
            dataMap.put("id", IdUtil.simpleUUID());
            dataList.add(dataMap);
        });
        halfList.forEach(ele -> {
            Map<String, Object> dataMap = new HashMap<>(3);
            dataMap.put("permissionId", ele);
            dataMap.put("roleId", id);
            dataMap.put("checked", false);//表示半选
            dataMap.put("id", IdUtil.simpleUUID());
            dataList.add(dataMap);
        });
        roleMapper.removeRolePermissionForRole(id);
        roleMapper.assignmentPermissionForRole(dataList);
    }

    /**
     * 启禁用角色
     *
     * @param roleId 角色id
     * @return java.lang.Object
     * @author Jzw
     * @date 2019/12/13 13:49
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void disOrEnaAccount(String roleId)
    {
        roleMapper.disOrEnaAccount(roleId);
    }

    /**
     * 获取当前角色已选的权限
     *
     * @param id 角色id
     * @return java.util.List<java.lang.String>
     * @author Jzw
     * @date 2019/12/17 16:21
     */
    @Override
    public List<String> listCheckedPermissionForRole(String id)
    {
        return roleMapper.listCheckedPermissionForRole(id);
    }

    /**
     * 获取用户关联的角色
     *
     * @param id 用户id
     * @return java.lang.Object
     * @author Jzw
     * @date 2019/12/30 11:09
     */
    @Override
    public String getRoleByUserId(String id)
    {
        return roleMapper.getRoleByUserId(id);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Override
    public RoleDO queryById(String id)
    {
        return roleMapper.queryById(id);
    }

    /**
     * 查询分页数据
     *
     * @param pagination 分页对戏
     * @return PageInfo 对象
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Override
    public PageInfo<RoleDO> queryAllByLimit(Pagination pagination)
    {
        PageHelper.startPage(pagination.getPageNum(),
                pagination.getPageSize());
        return new PageInfo<>(roleMapper.queryAllByLimit(JsonUtils.map2pojo(pagination.getParams(), RoleDO.class)));
    }

    /**
     * 查询全部数据
     *
     * @param role 筛选对象
     * @return 列表数据
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Override
    public List<RoleDO> queryAll(RoleDO role)
    {
        return roleMapper.queryAll(role);
    }

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 实例对象
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RoleDO insert(RoleDO role)
    {
        role.setCreateId(CurrentlyLoggedInUtil.getUserId());
        role.setEnable(true);
        roleMapper.insert(role);
        return role;
    }

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 实例对象
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(RoleDO role)
    {
        role.setUpdateId(CurrentlyLoggedInUtil.getUserId());
        return roleMapper.update(role);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(String id)
    {
        return roleMapper.deleteById(id) > 0;
    }

    /**
     * 通过主键集合批量删除数据
     *
     * @param idList 主键集合
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeletionByIdList(List<String> idList)
    {
        roleMapper.batchDeletionByIdList(idList);
    }
}